package com.vois.jack.btmgr.util;

import android.os.Bundle;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

public class TimeoutMonitor {
    private static final String TAG = "TimeoutMonitor";
    private Timer timeoutTimer;
    private Timer repeatTimer;
    private List<Integer> repeatDelays;
    private int timeout;
    private int repeatTry;
    private int currentTry;
    private Bundle arg;
    private MonitorCallback callback;

    public interface MonitorCallback {
        void doAction(Bundle arg);
        void onTimeout();
    }

    public TimeoutMonitor(int timeout, int repeatTry, int repeatDelay, Bundle arg, MonitorCallback callback) {
        this.timeout = timeout;
        this.repeatTry = repeatTry;
        repeatDelays = new ArrayList<>();
        for(int i = 0; i < repeatTry; i++) {
            repeatDelays.add(repeatDelay);
        }
        this.arg = arg;
        this.callback = callback;
        currentTry = 0;
    }

    public TimeoutMonitor(int timeout, int[] delays, Bundle arg, MonitorCallback callback) {
        this.timeout = timeout;
        repeatTry = delays.length;
        repeatDelays = new ArrayList<>();
        for (int i = 0; i < repeatTry; i++) {
            repeatDelays.add(delays[i]);
        }

        this.arg = arg;
        this.callback = callback;
        currentTry = 0;
    }

    public void resetStatus() {
        currentTry = 0;

        if (timeoutTimer != null) {
            timeoutTimer.cancel();
            timeoutTimer = null;
        }

        if (repeatTimer != null) {
            repeatTimer.cancel();
            repeatTimer = null;
        }
    }

    public void startMonitor() {
        if (timeoutTimer != null) {
            timeoutTimer.cancel();
            timeoutTimer = null;
        }

        timeoutTimer = new Timer();
        timeoutTimer.schedule(new TimerTask() {
            @Override
            public void run() {
                if(callback != null) {
                    callback.onTimeout();
                }
            }
        }, timeout);
    }

    public boolean doRepeat() {
        if(currentTry < repeatTry) {
            if (repeatTimer != null) {
                repeatTimer.cancel();
                repeatTimer = null;
            }

            repeatTimer = new Timer();
            repeatTimer.schedule(new TimerTask() {
                @Override
                public void run() {
                    startMonitor();
                    if(callback != null) {
                        callback.doAction(arg);
                    }
                }
            }, repeatDelays.get(currentTry));
            currentTry++;
            return true;
        }

        return false;
    }

    public void stopRepeat() {
        currentTry = 0;
        if(repeatTimer != null) {
            repeatTimer.cancel();
            repeatTimer = null;
        }
    }

    public void stopMonitor() {
        if(timeoutTimer != null) {
            timeoutTimer.cancel();
            timeoutTimer = null;
        }
    }
}
